﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmsVotes.DataAccess
{
    public class QuestionManager : BaseManager
    {
        public static Question GetByID(int questionID)
        {
            var question = (from item in EFContext.Questions where item.ID == questionID select item).FirstOrDefault();
            question.VoteReference.SmartLoad();
            question.ChartReference.SmartLoad();
            return question;
        }       
        /// <summary>
        /// Возвращает вопрос по его GUID
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static Question GetByGuid(Guid guid)
        {
            var question = (from item in EFContext.Questions where item.Guid.Equals(guid) select item).FirstOrDefault();
            return question;
        }
        /// <summary>
        /// Возвращает все типы графиков
        /// </summary>
        /// <returns></returns>
        public static List<Chart> GetAllCharts()
        {
            return EFContext.Charts.ToList();
        }
        /// <summary>
        /// Возвращает тип графика по ИД
        /// </summary>
        /// <param name="chartID"></param>
        /// <returns></returns>
        public static Chart GetChartByID(int chartID)
        {
            var chart = (from item in EFContext.Charts where item.ID == chartID select item).FirstOrDefault();
            return chart;
        }       
        /// <summary>
        /// Добавляет вопрос
        /// </summary>
        /// <param name="question"></param>
        public static void AddQuestion(Question question)
        {
            EFContext.AddToQuestions(question);         
        }
        /// <summary>
        /// Удаляет вопрос и связанные ответы
        /// </summary>
        /// <param name="question"></param>
        public static void Delete(Question question)
        {
            EFContext.DeleteObject(question);
            foreach (var answer in question.Answers)
                EFContext.DeleteObject(answer);
        }

    }
}
